/*
 * @(#)SearchProfileListener.java 0.0.1-SNAPSHOT
 *
 * Copyright Group B
 */

package grupb.upf.ms.listeners;

import grupb.upf.Controller.Controller;
import grupb.upf.ms.SearchProfile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

/**
 * <p>This component implements a PacketListener Interface. It receives
 * SearchProfile messages and inform to the user through the GUI about the
 * objects which matches the searched parameters.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public final class SearchProfileListener implements
        org.jivesoftware.smack.PacketListener {

    /**
     * The logger. A new logger <code>grupb.upf.ms.SearchProfileListener</code>
     * is created to add the logs of this class.
     */
    private static final Log LOG =
            LogFactory.getLog(SearchProfileListener.class);
    /**
     * The controller to send the changes.
     */
    private Controller controller;
   /**
    * Constructor of the listener.
    * @param c The controller
    */
    public SearchProfileListener(final Controller c) {
        this.controller = c;
    }

    /**
     * This method receives the packets which we want to receive (these packets
     * are filtered).
     * @param packet the packet
     */
    @Override
    public void processPacket(final Packet packet) {
        // Put the incoming message on the chat history or chat board.
        final Message msg = (Message) packet;
        final String from = msg.getFrom();
        if (LOG.isInfoEnabled()) {
            final StringBuffer sb =
                    new StringBuffer(msg.getPacketID())
                    .append(" received from ").append(from);
            LOG.info(sb);
        }
        final SearchProfile search =
                (SearchProfile) msg.getProperty("SearchProfile");
        controller.networkSearch(search.getSearchProfile(), from);
    }
}
